//
//  ViewController.m
//  TwitterUserCenterDemo
//
//  Created by 卢小辉 on 15/10/15.
//  Copyright © 2015年 lulee007. All rights reserved.
//

#import "ViewController.h"
#import "FXBlurView.h"

//header停止往上移动的距离
#define Offset_HeaderStop 40.0f
//黑色标签UserNickName到达header距离
#define Offset_B_LabelHeader 95.0f
//白色的标签UserName顶部到header底部距离
#define Distance_W_LabelHeader 35.0f

@interface ViewController ()

@property(nonatomic,strong )UIImageView *headerImageView;
@property(nonatomic,strong )UIImageView *headerBlurImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scorllView.delegate=self;
    
    
}

- (void)viewDidAppear:(BOOL)animated{

    //背景图
    self.headerImageView =[[UIImageView alloc]initWithFrame:self.headerView.bounds];
    UIImage *image=[UIImage imageNamed:@"header_bg@2x.jpg"];
    self.headerImageView.image=image;
    self.headerImageView.contentMode=UIViewContentModeScaleAspectFill;
    [self.headerView insertSubview:self.headerImageView belowSubview:self.headerLabel];

    //模糊图
    self.headerBlurImageView=[[UIImageView alloc]initWithFrame:self.headerView.bounds];
    self.headerBlurImageView.image=[[UIImage imageNamed:@"header_bg@2x.jpg"] blurredImageWithRadius:10 iterations:20 tintColor:[UIColor clearColor]];
    self.headerBlurImageView.contentMode=UIViewContentModeScaleAspectFill;
    self.headerBlurImageView.alpha=0.0f;
    [self.headerView insertSubview:self.headerBlurImageView belowSubview:self.headerLabel];
    
    self.headerView.clipsToBounds = true;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset=scrollView.contentOffset.y;
    CATransform3D avatarTransform=CATransform3DIdentity;
    CATransform3D headerTransform=CATransform3DIdentity;
    //下拉，header放大
    if(offset<0){
        CGFloat headerScaleFactor=(-offset)/self.headerView.bounds.size.height;
        CGFloat headerSizevariation=(self.headerView.bounds.size.height*headerScaleFactor)/2.0;
        headerTransform=CATransform3DTranslate(headerTransform, 0, headerSizevariation, 0);
        headerTransform=CATransform3DScale(headerTransform, 1.0+headerScaleFactor, 1.0+headerScaleFactor, 0);

    }
    //上拉
    else{
        
        //header 上移，当距离到达导航栏高度值时停止移动
        headerTransform=CATransform3DTranslate(headerTransform, 0, MAX(-Offset_HeaderStop, -offset), 0);
        
        //UserNickName 出现，上移到导航栏标题位置处
        CATransform3D labelTransform=CATransform3DMakeTranslation(0, MAX(-Distance_W_LabelHeader, Offset_B_LabelHeader-offset), 0);
        self.headerLabel.layer.transform=labelTransform;
        
        //模糊图 透明度
        self.headerBlurImageView.alpha=MIN(1.0, (offset-Offset_B_LabelHeader)/Distance_W_LabelHeader);
        
        //avatar头像变换，缩小，向上移动
        CGFloat avatarScaleFator=(MIN(Offset_HeaderStop, offset))/self.avatar.bounds.size.height/1.4;
        CGFloat avatarSizeVariation=avatarScaleFator*self.avatar.bounds.size.height/2.0;
        avatarTransform=CATransform3DTranslate(avatarTransform, 0, avatarSizeVariation, 0);
        avatarTransform=CATransform3DScale(avatarTransform, 1.0-avatarScaleFator, 1.0-avatarScaleFator, 0);
        
        //当上移到导航栏位置时，将headerview移到scrollview上层，
        if(offset<=Offset_HeaderStop){
            //下移
            if(self.avatar.layer.zPosition<self.headerView.layer.zPosition){
                self.headerView.layer.zPosition=0;
            }
        }else{
            //上移
            if(self.avatar.layer.zPosition>=self.headerView.layer.zPosition){
                self.headerView.layer.zPosition=2;
            }
        }
        
    }
    self.headerView.layer.transform=headerTransform;
    self.avatar.layer.transform=avatarTransform;
    
}

@end
